# 定义变量
base_url = 'http://localhost:8000'login_url = f'{base_url}/login'goods_url = f'{base_url}/goods'

# 登录表单数据
login_data = {
'username': 'your_username',
'password': 'your_password'}

# 测试登录
def test_login():
session = requests.Session()
response = session.post(login_url, data=login_data)
assert response.status_code == 302 # 期望状态码也可以是其他的，比如200

assert response.headers['Location'] == base_url + '/'
print('登录成功')
# 测试查询商品列表
def test_goods_list():
session = requests.Session()
session.post(login_url, data=login_data) # 登录

response = session.get(f'{goods_url}/list', params={'page': 1, 'rows': 10, 'goodsName': 'goods name'})
assert response.status_code == 200

assert len(response.json()['rows']) > 0

print('查询商品列表测试通过')
# 测试添加商品
def test_add_goods():
session = requests.Session()
session.post(login_url, data=login_data) # 登录

goods_data = {
    'goodsName': 'new goods name',
    'category': 'new category',
    'price': '100',
    'unit': 'new unit',
    'quantity': '200',
    'remarks': 'new remarks'
}
response = session.post(f'{goods_url}/save', json=goods_data)
assert response.status_code == 200

assert response.json()['status'] == 'success'
print('添加商品测试通过')
# 测试修改商品
def test_update_goods():
session = requests.Session()
session.post(login_url, data=login_data) # 登录

goods_id = 1 # 修改已存在的商品

goods_data = {
    'goodsId': goods_id,
    'goodsName': 'updated goods name',
    'category': 'updated category',
    'price': '150',
    'unit': 'updated unit',
    'quantity': '300',
    'remarks': 'updated remarks'
}
response = session.post(f'{goods_url}/save', json=goods_data)
assert response.status_code == 200

assert response.json()['status'] == 'success'
# 确认修改是否生效

updated_data_response = session.get(f'{goods_url}/list', params={'page': 1, 'rows': 10, 'goodsName': 'updated goods'})
assert updated_data_response.status_code == 200

assert len(updated_data_response.json()['rows']) > 0

updated_data = updated_data_response.json()['rows'][0]
assert updated_data['goodsId'] == goods_id

assert updated_data['goodsName'] == 'updated goods name'
assert updated_data['category'] == 'updated category'
print('修改商品测试通过')
# 测试删除商品
def test_delete_goods():
session = requests.Session()
session.post(login_url, data=login_data) # 登录

# 查询并选择要删除的商品ID

response = session.get(f'{goods_url}/list', params={'page': 1, 'rows': 10, 'goodsName': 'new goods name'})
assert response.status_code == 200

assert len(response.json()['rows']) > 0 # 这里假设已经有一条名为"new goods name"的记录

goods_id = response.json()['rows'][0]['goodsId']
# 发送删除请求

response = session.post(f'{goods_url}/delete', params={'ids': goods_id})
assert response.status_code == 200

assert response.json()['status'] == 'success'
# 再次查询以确认是否已被删除

deleted_data_response = session.get(f'{goods_url}/list', params={'page': 1, 'rows': 10, 'goodsName': 'new goods name'})
assert deleted_data_response.status_code == 200

assert len(deleted_data_response.json()['rows']) == 0

print('删除商品测试通过')
if name == 'main':
test_login()
test_goods_list()
test_add_goods()
test_update_goods()
test_delete_goods()






